//
//  HWItemMainListVC.m
//  HappyWeekend
//
//  Created by lanou on 16/1/12.
//  Copyright © 2016年 com.mjykj.www. All rights reserved.
//

#import "HWItemMainListVC.h"
#import "HWItemCollectionViewCell.h"
#import "HWItemDetailsViewController.h"


@interface HWItemMainListVC ()<UICollectionViewDataSource, UICollectionViewDelegate>

@property (nonatomic, strong) UICollectionView *collctionView;

@property (nonatomic, strong) NSMutableArray *dataArray;

@property (nonatomic, assign) NSInteger requestStart;

@end
static NSInteger number = 0;
@implementation HWItemMainListVC
- (void)viewWillAppear:(BOOL)animated{
    self.tabBarController.tabBar.hidden = NO;
}
- (void)viewDidLoad {
     _dataArray = [NSMutableArray array];
    _requestStart = 0;
    [super viewDidLoad];
    [self addColltionViwe];
    [self requestDataWithCount:0];
    self.navigationController.navigationBar.backgroundColor = [UIColor colorWithRed:58.0f / 255.0f green:201.0f / 255.0f blue:187.0f / 255.0f alpha:1];
    // 上拉刷新
    self.collctionView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        
        [self requestDataWithCount:0];
    }];
    // 下拉加载更多
        self.collctionView.mj_footer = [MJRefreshAutoFooter footerWithRefreshingBlock:^{
            if (number < 1) {
            _requestStart = 20;
            [self requestDataWithCount:20];
            number++;
            }
        }];
}
// 请求数据
- (void)requestDataWithCount:(NSInteger)count{
    NSString *str = @"http://api.guozhoumoapp.com/v2/items?gender=1&generation=2&limit=20&offset=";
    NSString *url = [NSString stringWithFormat:@"%@%ld",str,count];
    [HWRequestManager requestWithUrl:url parmDic:nil method:GET1 succeedWithData:^(NSData *data) {
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:(NSJSONReadingMutableContainers) error:nil];
        NSArray *array = dic[@"data"][@"items"];
        if (_requestStart == 0) {
            [_dataArray removeAllObjects];
        }
        for (NSDictionary *oneDic in array) {
            NSDictionary *newDic = oneDic[@"data"];
            HWItemModel *model = [[HWItemModel alloc] init];
            [model setValuesForKeysWithDictionary:newDic];
            [_dataArray addObject:model];
        }
        [self.collctionView.mj_header endRefreshing];
        [self.collctionView.mj_footer endRefreshing];
        [_collctionView reloadData];
    } failedWithError:^(NSError *connectionError) {
        
    }];
}
- (void)addColltionViwe{
    CGFloat width = kScreenWidth / 2 - 15;
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.minimumLineSpacing = 10;
    layout.minimumInteritemSpacing = 10;
    layout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
    layout.scrollDirection = UICollectionViewScrollDirectionVertical;
    layout.itemSize = CGSizeMake(kScreenWidth / 2 - 15, 376 * width / 481 + 76);
    _collctionView = [[UICollectionView alloc] initWithFrame:[UIScreen mainScreen].bounds collectionViewLayout:layout];
    _collctionView.dataSource = self;
    _collctionView.delegate = self;
    
    _collctionView.backgroundColor = [UIColor colorWithRed:235.0f / 255.0f green:235.0f / 255.0f blue:241.0f / 255.0f alpha:1];
    [self.view addSubview:_collctionView];
    [_collctionView registerClass:[HWItemCollectionViewCell class] forCellWithReuseIdentifier:@"HWItemCollectionViewCell"];
    
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return _dataArray.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    HWItemCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"HWItemCollectionViewCell" forIndexPath:indexPath];
    cell.backgroundColor = [UIColor whiteColor];
    cell.model = _dataArray[indexPath.row];
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    HWItemDetailsViewController *detailsVC = [[HWItemDetailsViewController alloc] init];
    HWItemModel *model = _dataArray[indexPath.row];
    detailsVC.itemModel = model;
    self.tabBarController.tabBar.hidden = YES;
    [self.navigationController pushViewController:detailsVC animated:YES];
}






- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
}



@end
